﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class ContentAdd : BaseCtrl
    {
        private string Act = YuRequest.GetQueryString("Act");
        private int PostID = YuRequest.GetQueryInt("PostID");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.OnStart();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected void OnStart()
        {
            if (Act == "edit")
            {
                this.InitUpdate(PostID);
                btnSave.Text = "修改文章";
            }
              
        }


        #region "添加"
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
               
                int cBigTypeID = YuRequest.GetFormInt(drpBigType.UniqueID, 0);
                int cSmallTypeID = YuRequest.GetFormInt(drpSmallType.UniqueID, 0);
                string cTitle = YuRequest.GetFormString(txtTitle.UniqueID);
                string cContent = YuRequest.GetFormString(txtContent.UniqueID);
                string cAllowTop = YuRequest.GetFormString(chkAllowTop.UniqueID);
                string cAllowComment = YuRequest.GetFormString(chkAllowComment.UniqueID);
                string cAllowShow = YuRequest.GetFormString(chkAllowShow.UniqueID);
                string cTags = YuRequest.GetFormString(txtTags.UniqueID);
                int cClick = YuRequest.GetFormInt(txtClick.UniqueID, 0);
                if (cBigTypeID == 0 || cSmallTypeID == 0)
                {
                    return;
                }
                Yu.Cms.Model.Content mContent = bllContent.GetModel(PostID);
                mContent.BigTypeID = cBigTypeID;
                mContent.SmallTypeID = cSmallTypeID;
                mContent.Title = cTitle;
                mContent.aContent = Utils.HtmlDecode(cContent);
                mContent.AllowComment = Utils.StrToBool(cAllowComment, false);
                mContent.AllowTop = Utils.StrToBool(cAllowTop, false);
                mContent.AllowShow = Utils.StrToBool(cAllowShow, false);

                mContent.aKey = cTags; //Tags
                mContent.ViewCount = cClick;

                int rInt = -1;
                if (Act == "edit")
                {
                    rInt= bllContent.Update(mContent);
                   
                }
                else
                {
                    mContent.PubTime = DateTime.Now;
                    rInt= bllContent.Add(mContent);
                  
                }

                JScript.AlertAndRedirect("操作成功", "Content.aspx");
                
            }
        }
        #endregion


        protected void InsertTags(string tags)
        {
           
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="PostID"></param>
        protected void InitUpdate(int PostID)
        {
            Yu.Cms.Model.Content mContent = bllContent.GetModel(PostID);
            cdrpBigType.SelectedValue = mContent.BigTypeID.ToString();
            cdrpSmallType.SelectedValue = mContent.SmallTypeID.ToString();
            txtTitle.Text = Utils.UrlDecode(mContent.Title);

            txtContent.Value = mContent.aContent;
            txtClick.Text = mContent.ViewCount.ToString();
            if (mContent.AllowComment)
            {
                chkAllowComment.Checked = true;
            }
            else
            {
                chkAllowComment.Checked = false;
            }
            if (mContent.AllowShow)
            {
                chkAllowShow.Checked = true;
            }
            else
            {
                chkAllowShow.Checked = false;
            }
            if (mContent.AllowTop)
            {
                chkAllowTop.Checked = true;
            }
            else
            {
                chkAllowTop.Checked = false;
            }
        }

        
    }
}